﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;

/// <summary>
/// 常用方法整理
/// </summary>
public class WebHelper
{
    public WebHelper()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    /// <summary>
    /// 获取客户端IP
    /// </summary>
    /// <returns></returns>
    public static string GetClientIp()
    {

        string user_IP = null;
        if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
        {
            user_IP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
        }
        else
        {
            user_IP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
        }
        return user_IP;

    }


    /// <summary>
    /// 获取服务器端IP
    /// </summary>
    /// <returns></returns>
    public static string GetServerIp()
    {
        return HttpContext.Current.Request.ServerVariables.Get("Local_Addr").ToString();

    }

    /// <summary>
    ///  页面输出Json信息
    /// </summary>
    /// <param name="strJson">输出对象</param>
    public static void HttpResponseJson(string strJson)
    {
        System.Web.HttpContext.Current.Response.ContentType = "text/plain";
        System.Web.HttpContext.Current.Response.Clear();
        System.Web.HttpContext.Current.Response.Write(strJson);
        System.Web.HttpContext.Current.Response.End();
    }

    /// <summary>
    /// 根据DataTable生成Json树结构
    /// </summary>
    /// <param name="tabel">数据源</param>
    /// <param name="idCol">ID列</param>
    /// <param name="txtCol">Text列</param>
    /// <param name="rela">关系字段</param>
    /// <param name="pId">父ID</param>
    /// <param name="pId">父列名</param>
    /// <param name="pId">菜单级别列名</param>
    public static string GetTreeJsonByTable(DataTable tabel, string idCol, string txtCol, string rela, object pId, string columnsName4parentCode, string columnsName4menuLevel)
    {
        StringBuilder result = new StringBuilder();
        StringBuilder sb = new StringBuilder();
        result.Append(sb.ToString());
        sb.Clear();
        if (tabel.Rows.Count > 0)
        {
            sb.Append("[");
            string filer = string.Format("{0}='{1}'", rela, pId);
            DataRow[] rows = tabel.Select(filer);
            if (rows.Length > 0)
            {
                foreach (DataRow row in rows)
                {
                    sb.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol].ToString().Trim() + "\",\"state\":\"open\"");
                    sb.Append(",\"attributes\":{\"parent_code\":\"" + row[columnsName4parentCode] + "\",\"menu_level\":\"" + row[columnsName4menuLevel].ToString().Trim() + "\"}");
                    if (tabel.Select(string.Format("{0}='{1}'", rela, row[idCol])).Length > 0)
                    {
                        sb.Append(",\"children\":");
                        sb.Append(GetTreeJsonByTable(tabel, idCol, txtCol, rela, row[idCol], columnsName4parentCode, columnsName4menuLevel));
                        result.Append(sb.ToString());
                        sb.Clear();
                    }
                    result.Append(sb.ToString());
                    sb.Clear();
                    sb.Append("},");
                }
                sb = sb.Remove(sb.Length - 1, 1);
            }
            sb.Append("]");
            result.Append(sb.ToString());
            sb.Clear();
        }

        return result.ToString();
    }
}